package com.xiaoyu.lanling.feature.moment.controller.detail

import `in`.srain.cube.views.list.CubeRecyclerViewSimpleAdapter
import `in`.srain.cube.views.list.PagedListDataModelWithRequestTag
import android.content.Context
import androidx.recyclerview.widget.RecyclerView
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout
import com.xiaoyu.base.event.AppEventBus
import com.xiaoyu.base.event.SimpleEventHandler
import com.xiaoyu.base.view.list.BaseListViewController
import com.xiaoyu.lanling.event.moment.detail.CommentListEvent
import com.xiaoyu.lanling.event.moment.detail.CommentMomentEvent
import com.xiaoyu.lanling.event.moment.detail.DeleteCommentEvent
import com.xiaoyu.lanling.feature.moment.model.detail.CommentItem
import com.xiaoyu.lanling.feature.moment.viewholder.detail.CommentItemViewHolder
import org.greenrobot.eventbus.Subscribe
import org.greenrobot.eventbus.ThreadMode

class CommentDetailListController(context: Context, listData: PagedListDataModelWithRequestTag<CommentItem, Any>, adapter: CubeRecyclerViewSimpleAdapter<CommentItem>, swipeRefreshLayout: SwipeRefreshLayout, recyclerView: RecyclerView) : BaseListViewController<CommentItem, Any>(context, listData, adapter, swipeRefreshLayout, recyclerView) {

    companion object {

        fun create(context: Context, listData: PagedListDataModelWithRequestTag<CommentItem, Any>, swipeRefreshLayout: SwipeRefreshLayout, recyclerView: RecyclerView): CommentDetailListController {
            val adapter = CubeRecyclerViewSimpleAdapter<CommentItem>()
            adapter.setViewHolderClass(0, null, CommentItemViewHolder::class.java, 20)

            val controller = CommentDetailListController(context, listData, adapter, swipeRefreshLayout, recyclerView)

            AppEventBus.bindContainerAndHandler(context, object : SimpleEventHandler() {

                @Subscribe(threadMode = ThreadMode.MAIN)
                fun onEvent(event: CommentListEvent) {
                    controller.onListEvent(event)
                }

                @Subscribe(threadMode = ThreadMode.MAIN)
                fun onEvent(event: DeleteCommentEvent) {
                    val index = listData.listPageInfo.dataList.indexOfFirst { it.comment.id == event.cid }
                    if (index != -1) {
                        listData.listPageInfo.dataList.removeAt(index)
                        adapter.notifyItemRemoved(index)
                        if (listData.listPageInfo.dataList.isNotEmpty()) {
                            // NOTE newly first comment has icon at start , so notify it
                            adapter.notifyItemChanged(0)
                        }
                    }
                }

                @Subscribe(threadMode = ThreadMode.MAIN)
                fun onEvent(event: CommentMomentEvent) {
                    val newComment = event.comment
                    listData.listPageInfo.dataList.add(newComment)
                    adapter.notifyItemInserted(listData.listPageInfo.dataList.lastIndex)
                }
            })
            return controller
        }
    }
}